import 'dart:async';

import 'package:aif_photo_plugin/photo_models/album_model.dart';
import 'package:aif_photo_plugin/photo_models/asset_model.dart';
import 'package:flutter/services.dart';

class AifPhotoPlugin {
  static const MethodChannel _channel = const MethodChannel('aif_photo_plugin');

  static Future<String> get platformVersion async {
    final String version = await _channel.invokeMethod('getPlatformVersion');
    return version;
  }

  static Future<List<AlbumModel>> getAllAlbums() async {
    final String method = "getAllAlbums";
    final List<dynamic> mapList = await _channel.invokeMethod(method) ?? [];
    final albumList = mapList.where(filterAction).map(albumAction).toList();
    return albumList;
  }

  static Future<List<AlbumModel>> getSmartAlbums() async {
    final String method = "getSmartAlbums";
    final List<dynamic> mapList = await _channel.invokeMethod(method) ?? [];
    final albumList = mapList.where(filterAction).map(albumAction).toList();
    return albumList;
  }

  static Future<List<AlbumModel>> getUserAlbums() async {
    final String method = "getUserAlbums";
    final List<dynamic> mapList = await _channel.invokeMethod(method) ?? [];
    final albumList = mapList.where(filterAction).map(albumAction).toList();
    return albumList;
  }

  static Future<AlbumModel> getAlbum(String albumId) async {
    if (albumId == null || albumId.length <= 0) {
      throw Exception("缺少相册标识符");
    }
    try {
      final String method = "getAlbum";
      final args = {
        "localIdentifier":albumId
      };
      final Map<String,dynamic> albumMap = await _channel.invokeMethod(method,args);
      final isValidMap = filterAction(albumMap);
      if (!isValidMap) {
        throw Exception("相册不存在");
      }
      return albumAction(albumMap);
    } catch (e) {
      throw e;
    }
  }

  static Future<List<AssetModel>> getAllAssets() async {
    final String method = "getAllAssets";
    final List<dynamic> assetMapList = await _channel.invokeMethod(method);
    final assetList = assetMapList.where(filterAction).map(assetsAction).toList();
    return assetList;
  }

  static Future<List<AssetModel>> getAlbumAssets(String albumId) async {
    if (albumId == null || albumId.length <= 0) {
      throw Exception("缺少相册标识符");
    }
    try {
      final String method = "getAlbumAssets";
      final args = {
        "localIdentifier":albumId
      };
      final List<dynamic> assetMapList = await _channel.invokeMethod(method,args);
      final assetList = assetMapList.where(filterAction).map(assetsAction).toList();
      return assetList;
    } catch (e) {
      throw e;
    }
  }

  static Future<AssetModel> getAsset(String assetId) async {
    if (assetId == null || assetId.length <= 0) {
      throw Exception("缺少相册标识符");
    }
    try {
      final String method = "getAsset";
      final args = {
        "localIdentifier":assetId
      };
      final assetMap = await _channel.invokeMethod(method,args);
      final validMap = filterAction(assetMap);
      if (!validMap) {
        Exception("资源不存在");
      }
      return assetsAction(assetMap);
    } catch (e) {
      throw e;
    }
  }

  static Future<String> getAssetPath(String assetId) async {
    if (assetId == null || assetId.length <= 0) {
      throw Exception("缺少相册标识符");
    }
    try {
      final String method = "getAssetPath";
      final args = {
        "localIdentifier":assetId
      };
      final path = await _channel.invokeMethod(method,args);
      return path;
    } catch (e) {
      throw e;
    }
  }
  static Future<String> getAssetThumbPath(String assetId) async {
    if (assetId == null || assetId.length <= 0) {
      throw Exception("缺少相册标识符");
    }
    try {
      final String method = "getAssetThumb";
      final args = {
        "localIdentifier":assetId
      };
      final path = await _channel.invokeMethod(method,args);
      return path;
    } catch (e) {
      throw e;
    }
  }
}

bool filterAction(element) {
  final validElement = new Map<String,dynamic>.from(element);
  final localIdentifier = validElement["localIdentifier"] as String;
  return localIdentifier != null && localIdentifier.length >= 1;
}

AlbumModel albumAction(json) {
  final validJson = new Map<String,dynamic>.from(json);
  final localIdentifier = validJson["localIdentifier"] as String;
  final albumItem = AlbumModel.fromJson(localIdentifier, validJson);
  return albumItem;
}

AssetModel assetsAction(json) {
  final validJson = new Map<String,dynamic>.from(json);
  final localIdentifier = validJson["localIdentifier"] as String;
  final assetItem = AssetModel.fromJson(localIdentifier, validJson);
  return assetItem;
}